<?php

// Prevent individual file access
if(!defined('DIR_BASE')) exit;

/**
 * The class DrawingModel contains functions for the table 'images'.
 */
class DrawingModel {
	/**
	* Instance of the table 'images'.
	*/
	private $tableImages;
	
	/**
	* Create a instance of the table 'images'.
	*/
	public function __construct() {
		$this->tableImages = new Table('images', array(
			'id',
			'wordid',
			'filetype',
			'url',
			'score',
			'web'
		));
	}
	
	/**
	 * Get a list of images
	 * @param Int $wordid Get images of the given wordid
	 */
	public function getImageList($word = null) {
		$qb = new QueryBuilder();
		if($word != null){
			$qry = $qb -> select('id')
					   -> table('words')
					   -> where('word', $word)
					   -> build();
			$wordid = Database::query($qry);
		}
		$qb -> select('id') -> table('images');
		if($word != null)
			$qb -> where('wordid', $wordid);
		else
			$qb -> select('wordid');
		
		$qry = $qb -> orderby('score') -> build();
		return Database::queryList($qry);
	}
	
	/**
	 * Add images
	 * @param String $word Add image of the specified word
	 * @param String $url Give an url if necessary (important when it is an image of the web)
	 * @param String $filetype Give a specified filetype of the image(important when it is an image of the web)
	 */
	public function add($word, $web = false, $url = '', $filetype = 'png') {
		$qb = new QueryBuilder();
		$qry = $qb 	-> select('id')
					-> table('words')
					-> where('word', $word)
					-> build();
		$wordid = Database::query($qry);
		if(empty($wordid)) $wordid = 0;
		$values = array(
			'wordid' => $wordid,
			'filetype' => $filetype,
			'web' => $web,
			'url' => $url
		);
		return $this->tableImages->store($values);
	}
	
	/**
	* Save the drawing that is made by the User.
	*/
	public function saveImage($id) {
		//BRON: http://permadi.com/blog/2010/10/html5-saving-canvas-image-data-using-php-and-ajax/ 05-06-2012
		if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
		    // Get the data
		    $imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
		 
		    // Remove the headers (data:,) part.  
		    // A real application should use them according to needs such as to check image type
		    $filteredData=substr($imageData, strpos($imageData, ",")+1);
		 
		    // Need to decode before saving since the data we received is already base64 encoded
		    $unencodedData=base64_decode($filteredData);
		 
		    //echo "unencodedData".$unencodedData;
		
		    // Save file.  This example uses a hard coded filename for testing, 
		    // but a real application can specify filename in POST variable
		    $fp = fopen('../drawings/'.$id.'.png', 'wb' );
		    fwrite( $fp, $unencodedData);
		    fclose( $fp );
			
		}
	}
	
	/**
	* Raise the score of the given image.
	* @param String $imgid	The id of the image.
	*/
	public function raiseScore($imgid) {
		$qb = new QueryBuilder();
		$qry = $qb 	-> update()
					-> table('images')
					-> where('id', $imgid)
					-> increase('score', 1)
					-> build();
		Database::query($qry);
	}
	
	/**
	 * Delete a specified image
	 */
	public function delete($imageid){
		//unlink('../drawings/'.$imgid.'.png');
		$this->tableImages->delete($imageid);
	}
}

?>